package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gfxy.modules.common.entity.MemberList;
import com.gfxy.modules.common.service.IMemberListService;
import com.gfxy.modules.items.entity.ProjectForYear;
import com.gfxy.modules.items.mapper.ProjectForYearMapper;
import com.gfxy.modules.items.service.IProjectForYearService;
import com.gfxy.modules.items.vo.ProjectForYearWithMemberList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 测试关联
 * @Author: jeecg-boot
 * @Date:   2021-10-20
 * @Version: V1.0
 */
@Service
public class ProjectForYearServiceImpl extends ServiceImpl<ProjectForYearMapper, ProjectForYear> implements IProjectForYearService {

    @Autowired
    private IMemberListService memberListService;


    @Override
    public boolean saveList(String projectId, List<ProjectForYearWithMemberList> yearMemberListList) {

        for (ProjectForYearWithMemberList projectForYearWithMemberList : yearMemberListList) {
            projectForYearWithMemberList.setProjectId(projectId);
            this.saveOne(projectForYearWithMemberList);
        }
        return true;
    }

    private boolean saveOne(ProjectForYearWithMemberList projectForYearWithMemberList){

        ProjectForYear projectForYear = new ProjectForYear();
        BeanUtils.copyProperties(projectForYearWithMemberList,projectForYear);
        this.save(projectForYear);
        List<MemberList> memberListList = projectForYearWithMemberList.getMemberList();
        memberListService.saveList(projectForYear.getId(), memberListList);
        return true;
    }

    public boolean updateList(String projectId, List<ProjectForYearWithMemberList> yearMemberListList){

        for (ProjectForYearWithMemberList projectForYearWithMemberList : yearMemberListList) {
            if(projectForYearWithMemberList.getState() == 1){

                memberListService.deleteByItemId(projectForYearWithMemberList.getId());
                this.baseMapper.deleteById(projectForYearWithMemberList.getId());

                projectForYearWithMemberList.setProjectId(projectId);
                this.saveOne(projectForYearWithMemberList);
            }
        }
        return true;
    }

//    @Override
//    public List<ProjectForYear> listMainid(QueryWrapper<ProjectForYear> yearQueryWrapper) {
//        return baseMapper.selectList(yearQueryWrapper);
//    }

    @Override
    public List<ProjectForYear> queryProjectForYearByProjectId(String projectId) {
        QueryWrapper<ProjectForYear> yearQueryWrapper = new QueryWrapper<>();
        yearQueryWrapper.eq("project_id",projectId);
        return this.baseMapper.selectList(yearQueryWrapper);
    }

    @Override
    public void deleteList(List<ProjectForYear> projectForYears) {
        for (ProjectForYear projectForYear : projectForYears) {
            this.memberListService.deleteByItemId(projectForYear.getId());
        }
        this.removeByIds(projectForYears);
    }


    public List<ProjectForYear> test(QueryWrapper<ProjectForYear> queryWrapper){
        return baseMapper.selectList(queryWrapper);
    }


}
